//
//  MainViewController.swift
//  WeiboProject
//
//  Created by yanli on 16/7/9.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit
import SVProgressHUD

class SKMainViewController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 创建 撰写Btn
        let skTabBar = SKTabBar()
        
        // 使用另一种方法 代理方法.遵守代理协议,在下面执行代理方法
        skTabBar.skDelegate = self
        
        
        // 使用 SKTabBar 中定义的闭包  使用self会产生循环引用
        skTabBar.composeBtnClosure = { [weak self] in
            
            // 判断如果没有登录,点击撰写按钮,会让 用户 先登录
            if !SKUserAccountViewModel.shareUserAccount.isLogin {
                SVProgressHUD.showErrorWithStatus("请先登录,亲~")
                return
            }
            //  代码执行到此表示已经登录
            
            //  这里存在循环引用的问题
            if let weakSelf = self {
                let composeView = SKComposeView()
                composeView.show(weakSelf)
            }
            
            //  获取最上面的一个window (小技巧)    --> 这里在 composeView.show 方法里面实现了
//            let window = UIApplication.sharedApplication().windows.last!
//            
//            window.addSubview(composeView)
        }
        
        
        // 使用kvc给只读属性设置值  不能直接设置  self.tabBar = czTabbar
        setValue(skTabBar, forKey: "tabBar")
        
        //self.tabBar = czTabbar  不能直接设置
        
        //添加 子控制器 并设置
        addChildViewController(SKHomeTableViewController(),title: "首页", imageName: "tabbar_home")
        addChildViewController(SKMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(SKDIscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(SKMyTableViewController(), title: "我的", imageName: "tabbar_profile")
    }
    
    
    func addChildViewController(childController: UIViewController,title: String,imageName: String) {
        
        //  设置自定义tabBarItem
        childController.tabBarItem = SKTabBarItem()
        
        
        //  设置tabar的文字和显示图片
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        //  设置指定渲染模式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //  设置tabbar字体颜色(选中状态)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        // 设置字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(15)], forState: .Normal)
        
        //  添加子视图控制器
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
       }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}



// MARK:   实现SKTabBarDelegate代理协议
extension SKMainViewController:SKTabBarDelegate {
    func didSelectedComposeBtn() {
        print("撰写按钮点击,代理实现",self)
    }
}



